﻿using System;
using System.Collections;

namespace TomNet.Attributes
{
    /// <summary>
    /// 非空监测
    /// </summary>
    public class RequiredAttribute : BaseAttribute
    {
        public override string Error
        {
            get
            {
                if (base.Error != null)
                {
                    return base.Error;
                }
                return "不能为空";
            }

            set => base.Error = value;
        }
        public override bool Validate(object value)
        {
            return value switch
            {
                null => false,
                string str => str.Length > 0,
                Array arr => arr.Length > 0,
                IEnumerable eum => eum.GetEnumerator().MoveNext(),
                _ => true
            };
        }
    }
}
